package dataextract4collabfiltering;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestMatrix {

	public static void main (String[] args) throws Exception{
		
		Map<String, List<String>>map = new HashMap<String, List<String>>();
		Map<String, List<Integer>>map2 = new HashMap<String, List<Integer>>();
	   	
		//String POI = "biblioteca nazionale,casa dei romagnano,casa del pingone,castello del valentino,chiesa della gran madre,chiesa di san domenico,duomo di torino,galleria subalpina,il museo egizio,il palazzo reale,la galleria sabauda,la villa della reggina,lingotto,mole antonelliana,museo nazionale del cinema,opera per torino,pala torino,palazzo carignano,palazzo madama,palla rufini,plazzina di stupinigi,politecnico di torino,porta palatina,reggia di venaria,teatro reggio,teatro romano,"; 
		//String [] POI_array = POI.split(",");
	   	String file = "C:\\Users\\Alket\\junocode\\FlickrAPI\\PuntiInteresseTorino.txt";
	   	 String file2 = "C:\\Users\\Alket\\junocode\\FlickrAPI\\POIPerPLSUserImsiFile_2sett.csv";
	   	 loadData2Map2(map, file2);
	   	 List<String> list = new ArrayList<String>();
	    	loadData(file, list);
	   	
	     	for (int i = 0; i < list.size(); i++) {		
		      System.out.print(list.get(i)+",");
	   	    }
	   	System.out.println();
	   	
	   	Collections.sort(list);
	   	System.out.println(list);
	   	printMatrix("matrix_1sett.csv", map, list);
			for (String s: map.keySet()) {
				//System.out.print(s+": ");
				//for (int j = 0; j < map.get(s).size(); j++) {
					for (int i = 0; i < list.size(); i++) {
						String str_i = list.get(i);
					if(map.get(s).contains(str_i)){
						System.out.print("\t\t"+1+",");
					}
					else System.out.print("\t\t"+0+",");
				}
				System.out.println();
			}
		}
	
	
	public static void loadData(String file, List<String> list) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			
			String line ;
			while((line = br.readLine())!= null){
				String [] riga = line.split(",");
				
				list.add(riga[0]);
				
	    	}br.close();
		Collections.sort(list);
	}
	
	public static void loadData2Map(Map<String, List<Integer>> list, String file) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
		 List<Integer> in_t = null;
			String line ;
			while((line = br.readLine())!= null){
				in_t = new ArrayList<Integer>();
				String [] riga = line.split(": ");
				
				String usernameimsi = riga[0];
				String[]poi = riga[1].split(", ");
				for (int i = 0; i <poi.length ; i++) {
					int iesimo = Integer.parseInt(poi[i]);
					in_t.add(iesimo);
				}
				list.put(usernameimsi, in_t);
	    	}br.close();
	}
	
	public static void loadData2Map2(Map<String, List<String>> list, String file) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
		 List<String> in_t = null;
			String line ;
			while((line = br.readLine())!= null){
				in_t = new ArrayList<String>();
				String [] riga = line.split(":");
				
				String usernameimsi = riga[0];
				//System.out.println(usernameimsi);
				String[]poi = riga[1].split(",");
				for (int i = 0; i <poi.length ; i++) {
					
					in_t.add(poi[i]);
				}
				list.put(usernameimsi, in_t);
				
	    	}br.close();
		
	}
     public static void printMatrix(String file, Map<String, List<String>>map , List<String>list)throws Exception{
    	
    	PrintWriter out = new PrintWriter(new FileWriter(file));
    	out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
    	for (int i = 0; i < list.size(); i++) {
    		out.print("\t"+list.get(i).toString()+"\t,");	
		}
    	out.println();
    	for(String l: map.keySet()){
    		out.print(l+"\t\t\t");
    		
    			for (int i = 0; i < list.size(); i++) {
				   String str_i = list.get(i);
				     if(map.get(l).contains(str_i)){
				      //out.print(" ");
					   out.print("\t\t\t "+1+"\t\t\t");
					   //out.print("  ");
				     }
				     else 
				    	// out.print(" ");
				    	 out.print("\t\t\t "+0+"\t\t\t");
				    	 //out.print("  ");
			    }
    		    out.println();
				//out.println(map.get(l).get(i).ts);
    	}
    	out.close();
    }
}
